home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 1993 May / Info-Mac_II_May_1993.to_.sit / Info-Mac II (May 1993).toast / Unix / Macbin.shar < prev    next >
Internet Message Format  |  1992-08-29  |  10KB

  1. Date: Thu, 24 Jan 91 12:05:17 CST
  2. From: 231b3679@fergvax.unl.edu (CS 231 section 2)
  3. Subject: macbin.shar
  4.  
  5. #!/bin/sh
  6. # to extract, remove the header and type "sh filename"
  7. if `test ! -d ./Macbin`
  8. then
  9.   mkdir ./Macbin
  10.   echo "mkdir ./Macbin"
  11. fi
  12. if `test ! -s ./Macbin/macbin_readme`
  13. then
  14. echo "writing ./Macbin/macbin_readme"
  15. cat > ./Macbin/macbin_readme << '\End\Of\Shar\'
  16. Macbin Documentation
  17. --------------------
  18.  
  19.    Purpose:  Convert .data, .info, and .rsrc files into one .bin
  20.       file for easy downloading (etc).
  21.       
  22.             MyMacFile.data  \
  23.             MyMacFile.info   >  MyMacFile.bin
  24.             MyMacFile.rsrc  /
  25.  
  26.    How to Use:
  27.       Compile the macbin.c source code file.  The easiest way to do
  28.       that would be to use the included Makefile.  Just type "Make"
  29.       at your unix host's prompt, and that should do it.
  30.            [Power user's note: if you have the Gnu C compiler
  31.             available, try to use it.  It will produce a substantially
  32.             smaller executable.  Type "make gcc" for that version.]
  33.       Ok, now let's assume after a "ls" you have this stuff in the
  34.       current directory:
  35.  
  36.       % ls
  37.       Makefile   a.sit.bin  macbin     macbin.c
  38.  
  39.       Let's also assume you have the nifty "unsit" executable to
  40.       decompress Stuffit files, and you type:
  41.  
  42.       % unsit a.sit.bin
  43.       % ls
  44.       Makefile                      Validator_documentation.info
  45.       Validator_1.11.data           Validator_documentation.rsrc
  46.       Validator_1.11.info           a.sit.bin
  47.       Validator_1.11.rsrc           macbin
  48.       Validator_documentation.data  macbin.c
  49.  
  50.       Now you want to download the decompressed files... but, you
  51.       need to combine Validator_1.11.data, Validator_1.11.info,
  52.       and Validator_1.11.rsrc into a single file, not 3 files.
  53.       Here's where macbin comes in.
  54.  
  55.       % macbin -d *
  56.  
  57.       You'll get a few messages from macbin, mostly telling you
  58.       that it didn't macbin-ize some files because there weren't
  59.       corresponding .data, .info., and .rsrc files:
  60.  
  61.       No Makefile.info file!
  62.       No a.sit.bin.info file!
  63.       No macbin.info file!
  64.       No macbin.c.info file!
  65.  
  66.       Those files won't be altered by macbin.  Do a "ls" however,
  67.       and you will find that those .data, .info, and .rsrc files
  68.       have been combined into one .bin file.  Those three files
  69.       were also deleted afterwards, since you passed "-d". I
  70.       recommend passing "-d" since you really have no use for those
  71.       other files anyway.
  72.  
  73.       % ls
  74.       Makefile                     a.sit.bin
  75.       Validator_1.11.bin           macbin
  76.       Validator_documentation.bin  macbin.c
  77.  
  78.       Notice now how we have our unstuffed files in .bin form, so
  79.       we can download them (or whatever).
  80.  
  81.    Version History:
  82.       1.0      Jim Budler        First release.  Handled one file
  83.                jimb@amdcad.UUCP  only, and didn't have the ability
  84.                                  to delete the .d, .i, .r files.
  85.       
  86.       2.0      Mike Gleason      Added batch files, option to delete
  87.                                  the .d, .i, .r, files after
  88.                                  processing.
  89.                            
  90.       3.0      Mike Gleason      Improved batch file handling, so
  91.                                  the user wouldn't need to type
  92.                                  "macbin file1 file2 file3 .. filen"
  93.                                  but instead "macbin *". Obviously,
  94.                                  in previous versions using "macbin *"
  95.                                  would assume we had files like
  96.                                  file1.data.data, file1.data.info,
  97.                                  file1.data.rsrc, file1.info.data...
  98.                                  One would realize the great
  99.                                  convenience of this feature if you
  100.                                  have had to unsit a whole buttload
  101.                                  of files, and then type in mile-long
  102.                                  command lines.
  103.  
  104.       3.0.1    Mike Gleason      Fixed silly bug to work with old
  105.                                  style function declarations, so one
  106.                                  could compile with cc instead of gcc.
  107.                         
  108. */
  109.  
  110.                         
  111. /* structure of the info file - from MacBin.C
  112.    char zero1;
  113.    char nlen;
  114.    char name[63];
  115.    char type[4];     65 0101
  116.    char creator[4];  69
  117.    char flags;    73
  118.    char zero2;    74 0112
  119.    char location[6]; 80
  120.    char protected;      81 0121
  121.    char zero3;    82 0122
  122.    char dflen[4];
  123.    char rflen[4];
  124.    char cdate[4];
  125.    char mdate[4];
  126. */
  127.  
  128. /* eof */
  129. \End\Of\Shar\
  130. else
  131.   echo "will not over write ./Macbin/macbin_readme"
  132. fi
  133. if `test ! -s ./Macbin/macbin.c`
  134. then
  135. echo "writing ./Macbin/macbin.c"
  136. cat > ./Macbin/macbin.c << '\End\Of\Shar\'
  137.  
  138. /* #define SYSV */
  139. /* 
  140.    Note:  to run this program on a System V Unix system, remove all calls to
  141.    the bzero() procedure.  It doesn't exist on System V, and isn't needed.
  142.    [To do this easily, just uncomment the above line, and the lines
  143.     containing bzero() will not be compiled. --MG] */
  144.  
  145. #include <stdio.h>
  146.  
  147.  
  148. main (argc, argv)
  149.    int argc;
  150.    char **argv;
  151. {
  152.    int i, deleteOn = 0;
  153.    
  154.    if (argc < 2) 
  155.    {
  156.         fprintf(stderr, "\nUsage: %s [-d] <list of files>\n", argv[0]);
  157.         fprintf(stderr, "\nPurpose: merges <prefix>.info, <prefix>.data, <prefix>.rsrc");
  158.         fprintf(stderr, "\n  into a valid macbinary file, <prefix>.bin, and optionally");
  159.         fprintf(stderr, "\n  deletes the .info, .data, .rsrc files (the -d).\n\n");
  160.       exit(1);
  161.    }
  162.    
  163.    if (argc > 3)
  164.       FilterArgumentList(argc, argv);
  165.    
  166.    for (i=1; i<argc; i++)
  167.       if (argv[i][0] == '-' && argv[i][1] == 'd')
  168.          deleteOn = !deleteOn;   /* you can toggle delete mode on/off */
  169.       else
  170.          if (argv[i][0] != '\0')  /* if the first character is not 0 */
  171.             macbin(argv[i], deleteOn);
  172. }  /* main */
  173.  
  174.  
  175.  
  176.  
  177.  
  178. FilterArgumentList(argc, argv)
  179.    int argc;
  180.    char **argv;
  181. {
  182.    register int i;
  183.    
  184.    for (i=1; i<argc-2; i++)
  185.    {
  186.         /* Compare the last five characters of the arguments to tell if
  187.             the user passed a generic wildcard. */
  188.       if    (LastFive(argv[i], ".data") &&
  189.                  LastFive(argv[i+1], ".info") &&
  190.                  LastFive(argv[i+2], ".rsrc")
  191.                  )
  192.       {
  193.          /* if 3 successive arguments contain .data,
  194.             .info, and .rsrc, (i.e. we have MyFkey.data,
  195.             MyFkey.info, and MyFkey.rsrc) then let's really
  196.             only pass the prefix (i.e. "MyFkey") */
  197.             
  198.          argv[i][ strlen(argv[i]) - 5 ] = 
  199.             argv[i+1][0] = argv[i+2][0] = '\0';
  200.             
  201.          /* we'll use the first character of an argument as a
  202.             signal to ignore it. It's a kludge, but it works. */
  203.       }
  204.    }
  205. }  /* FilterArgumentList */
  206.  
  207.  
  208.  
  209. LastFive(a, b)
  210.     char *a, *b;
  211. {
  212.    register char *c = a;
  213.  
  214.     /* this oddball routine compares the last five characters of a with
  215.         the first 5 characters of b. */
  216.     while (*c) c++;  /* find the end of the string */
  217.  
  218.     if (*(c-5) == *(b)   &&
  219.          *(c-4) == *(b+1) &&
  220.          *(c-3) == *(b+2) &&
  221.          *(c-2) == *(b+3) &&
  222.          *(c-1) == *(b+4)
  223.          )
  224.              return (1);  /* yup, they were equal */
  225.     else
  226.              return (0);  /* nope, at least one was different */
  227. } /* LastFive */
  228.  
  229.  
  230.  
  231.  
  232. macbin (prefix, delete)
  233.    char *prefix;
  234.    int  delete;
  235. {
  236.    FILE *fd, *ofd;
  237.    char oname[128];
  238.    char iname[128];
  239.    char dname[128];
  240.    char rname[128];
  241.    char buf[128];
  242.    
  243. #ifndef SYSV
  244.    bzero(buf, 128);
  245. #endif
  246.  
  247.    strcpy(oname, prefix);
  248.    strcat(oname, ".bin");
  249.    
  250.    if ((ofd = fopen( oname, "w")) == NULL)
  251.    {
  252.       fprintf(stderr, "\n Cannot open %s for writing.\n", oname);
  253.       return(1);
  254.    }
  255.    
  256.    strcpy(iname, prefix);
  257.    strcat(iname, ".info");
  258.    if ((fd = fopen(iname, "r")) == NULL)
  259.    {
  260.       fprintf(stderr, "No %s file!\n", iname);
  261.       fclose(ofd);
  262.       unlink(oname);
  263.       return(1);
  264.    }
  265.    
  266.    if (fread(buf, sizeof(*buf), 128, fd) > 0)
  267.    {
  268.       if (buf[74] & 0x40) buf[81] = '\1'; /* protected */
  269.       buf[74] = '\0'; /* clear zero2 */
  270.       fwrite(buf, sizeof(*buf), 128, ofd);
  271. #ifndef SYSV
  272.       bzero(buf, 128);
  273. #endif
  274.    }
  275.    fclose(fd);
  276.    
  277.    strcpy(dname, prefix);
  278.    strcat(dname, ".data");
  279.    if ((fd = fopen(dname, "r")) == NULL)
  280.    {
  281.       fprintf(stderr, "No %s file!\n", dname);
  282.       fclose(ofd);
  283.       unlink(oname);
  284.       return(1);
  285.    }
  286.    
  287.    while (fread(buf, sizeof(*buf), 128, fd) > 0)
  288.    {
  289.       fwrite(buf, sizeof(*buf), 128, ofd);
  290. #ifndef SYSV
  291.       bzero(buf, 128);
  292. #endif
  293.    }
  294.    fclose(fd);
  295.    
  296.    strcpy(rname, prefix);
  297.    strcat(rname, ".rsrc");
  298.    if ((fd = fopen(rname, "r")) == NULL)
  299.    {
  300.       fprintf(stderr, "No %s file!\n", rname);
  301.       fclose(ofd);
  302.       unlink(oname);
  303.       return(1);
  304.    }
  305.    while (fread(buf, sizeof(*buf), 128, fd) > 0)
  306.    {
  307.       fwrite(buf, sizeof(*buf), 128, ofd);
  308. #ifndef SYSV
  309.       bzero(buf, 128);
  310. #endif
  311.    }
  312.    fclose(fd);
  313.    
  314.    if (delete)
  315.    {
  316.       unlink(iname);
  317.       unlink(rname);
  318.       unlink(dname);
  319.    }
  320. }  /* macbin */
  321.  
  322. /* EOF */
  323. \End\Of\Shar\
  324. else
  325.   echo "will not over write ./Macbin/macbin.c"
  326. fi
  327. if `test ! -s ./Macbin/Makefile`
  328. then
  329. echo "writing ./Macbin/Makefile"
  330. cat > ./Macbin/Makefile << '\End\Of\Shar\'
  331.  
  332. # Compile with Gnu C, if possbile; it produced 20% smaller code in this case.
  333.  
  334. all:
  335.     cc -O macbin.c -o macbin
  336.  
  337. cc:
  338.     cc -O macbin.c -o macbin
  339.  
  340. gcc:
  341.     gcc -O macbin.c -o macbin
  342.  
  343. shar:
  344.     shar -f macbin_readme macbin.c Makefile > macbin.shar
  345.  
  346. \End\Of\Shar\
  347. else
  348.   echo "will not over write ./Macbin/Makefile"
  349. fi
  350. echo "Finished archive 1 of 1"
  351. exit
  352.  
  353.